<?php
class Provincia extends Doctrine_Record {
    public function setTableDefinition() {
        $this->setTableName('provincia');
        $this->hasColumn('contenido','string',255);
        $this->hasColumn('modificado','integer'); //sirve para trabajar con la cabecera HTTP_IF_MODIFIED_SINCE
        $this->hasColumn('id_region','integer');
    }

    public function setUp(){
        $this->hasOne('Region as region',array(
            'local'=>'id_region',
            'foreign'=>'id'
        ));
        $this->hasMany('Comuna as comunas',array(
            'local'=>'id',
            'foreign'=>'id_provincia'
        ));
    }

    //INCIO DE GETERS Y SETERS
    public function getContenido () {
        return utf8_decode($this->_get('contenido'));
    }
    public function setContenido ($contenido) {
        $this->_set('contenido', utf8_encode($contenido));
    }
    public function getComunas () {
        $q = Doctrine_Query::create()
            ->select('l.*')
            ->from('comuna l')
            ->where('l.id_provincia = '.$this->id)
            ->orderBy('l.contenido');
        return $q->execute();
    }
    //FIN SETERS Y GETERS
    
    public static function toSelect ($idRegion, $objeto=false) {
        $html = '<select id="selectProvincia" name="provincia">';
        $region = Doctrine::getTable('region')->find($idRegion);
        $html .= '<option selected="selected" value="">Elegir</option>';
        $idDepto = ($objeto)?$objeto->comuna->provincia->id:0;
        foreach ($region->provincias as $depto) {
            if (!$depto->id)continue;
            $selected = ($depto->id == $idDepto)?'  selected="selected"':'';
            $html .= '<option value="'.$depto->id.'"'.$selected.'>'.$depto->contenido.'</option>';
        }
        $html .= '</select>';
        return $html;
    }  
}
?>